package com.example.ApiController;

import com.example.common.Result;
import com.example.service.ScoreService;
import com.example.utils.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/email")
public class EmailApiController {


    @Autowired
    private EmailService emailService;
    @Autowired
    private ScoreService scoreService;

    @RequestMapping("/send")
    public Result send(Integer studentId) {
        try{
            List<Map<String,Object>> list = scoreService.selectByIStudentId(studentId);
            Object sname = list.get(0).get("sname");
            Object email = list.get(0).get("email");
            String s = email.toString();
            String subject = sname + "同学的成绩通知";
            StringBuilder sb = new StringBuilder();
            sb.append(sname + "同学的成绩："+ "\n");
            list.forEach(map->{
                Object cname = map.get("cname");
                Object score = map.get("score");
                sb.append(cname + "：" + score + "\n");
            });
            System.out.println(sb.toString());
            String string = sb.toString();
            emailService.sendEmail(s, subject, string);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
